package cn.com.shaom.learning.sb.web.tomcat;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * ClassName: CustomServletContainerConfig
 * Description: 通用servlet 容器配置
 * Date: 2017/12/22 14:39 【需求编号】
 *
 * @author Shaom
 * @version V1.0.0
 */
@Component
public class CustomServletContainerConfig implements EmbeddedServletContainerCustomizer {

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {

        TomcatEmbeddedServletContainerFactory factory = (TomcatEmbeddedServletContainerFactory) container;

        factory.setPort(8888);
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
        factory.setSessionTimeout(10, TimeUnit.HOURS);

        factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
                //设置最大连接数
                protocol.setMaxConnections(2000);
                //设置最大线程数
                protocol.setMaxThreads(2000);
                protocol.setConnectionTimeout(30000);
            }

        });

    }
}
